<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
    </div>
    <!--表格渲染-->
    <el-table v-loading="loading" ref="table"  :data="data" size="small" style="width: 100%;" border>
      <el-table-column type="selection" width="55"/>
      <el-table-column prop="processDefinitionName" label="流程名称"/>
      <el-table-column prop="id" label="执行ID"/>
      <el-table-column prop="processInstanceId" label="流程实例ID"/>
      <el-table-column prop="processDefinitionId" label="流程定义ID"/>
      <el-table-column prop="activityId" label="当期环节"/>
      <el-table-column prop="suspended" label="是否挂起"/>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button v-permission="['ADMIN','ACT_PROCESS_ALL','ACT_PROCESS_DISCARD']" size="mini" type="text" @click="discard(scope.row.processInstanceId)">[作废]</el-button>
          <el-button size="mini" type="text" @click="history(scope.row.processDefinitionId,scope.row.id)">[历史]</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--查看流程历史-->
    <el-dialog :visible.sync="dialog" title="查看流程历史" append-to-body top="0" width="85%">
      <img :src="flowImge" width="100%">
    </el-dialog >
    <!--作废原因-->
    <Discard ref="discardFrom" :process-instance-id="processInstanceId"/>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
import Discard from './discard'
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { getImage } from '@/api/framework/flow/flowProcess'

export default {
  components: {
    Discard
  },
  mixins: [initData],
  data() {
    return {
      dialog: false,
      flowImge: '',
      processInstanceId: ''
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    checkPermission,
    beforeInit() {
      this.url = 'flowProcess/runningList'
      this.params = { page: this.page, size: this.size }
      return true
    },
    history(processDefinitionId, id) {
      this.dialog = true
      getImage(processDefinitionId, id).then(res => {
        this.flowImge = 'data:image/png;base64,' + btoa(
          new Uint8Array(res)
            .reduce((data, byte) => data + String.fromCharCode(byte), '')
        )
      }).catch(err => {
        this.dialog = false
        console.log(err.response.data.message)
      })
    },
    discard(processInstanceId) {
      this.processInstanceId = processInstanceId
      this.$refs.discardFrom.dialogTableVisible = true
    }
  }
}
</script>

